package com.project.service;

import com.project.common.utils.MyPageHelper;
import com.project.dao.CourseMapper;
import com.project.dao.TeacherInfoMapper;
import com.project.entity.Course;
import com.project.entity.CourseTeach;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(rollbackFor = {Exception.class})
@Service
public class CourseServiceImpl extends BaseService implements CourseService {

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    CourseTeachService courseTeachService;

    @Autowired
    TeacherInfoMapper teacherInfoMapper;

    @Override
    public boolean add(Course course){
        courseMapper.insertSelective(course);
        if(course.getId()!=null&&course.getCreatorid()!=null) {
            CourseTeach courseTeach = new CourseTeach();
            courseTeach.setCourseid(course.getId());
            courseTeach.setTeacherid(course.getCreatorid());
            courseTeach.setIscreator(1);
            courseTeachService.add(courseTeach);
            return true;
        }
        return false;
    }

    @Override
    public Course findById(Integer id) {
        return courseMapper.selectByPrimaryKey(id);
    }

    /**
     * @author  wwh
     * @date 2018/04/16
     * @function 查找课程带创建者和维护者的详情
     * @param id
     * @return
     */
    @Override
    public Course findDetailById(Integer id) {
        Course course = courseMapper.selectByPrimaryKey(id);
        course.setCreator(teacherInfoMapper.selectDetailByTeacherid(course.getCreatorid()));
        course.setMaintainer(teacherInfoMapper.selectDetailByTeacherid(course.getMaintainerid()));
        return course;
    }

    @Override
    public List<Course> findByCondition(Course course) {
        MyPageHelper.setPageParam(course);
        return courseMapper.selectBatchByCondition(course);
    }

    @Override
    public List<Course> selectBykeyword(String name) {
        return courseMapper.selectBykeyword(name);
    }

    @Override
    public List<Course>selectByCreatorId(Integer creatorid) {
        return courseMapper.selectByCreatorId(creatorid);
    }
}
